package com.vf.admin.miaodong.controller;

import com.vf.admin.client.common.ClientConstants;
import com.vf.admin.client.common.RedisKeys;
import com.vf.admin.client.service.AppFamilyMemberService;
import com.vf.admin.client.service.AppFamilyService;
import com.vf.admin.client.service.AppFamilySetLevelService;
import com.vf.admin.client.service.AppUserService;
import com.vf.admin.client.tx.vo.UserShiZhi;
import com.vf.admin.common.BaseController;
import com.vf.admin.common.entity.Result;
import com.vf.admin.common.splitpage.SplitPageBean;
import com.vf.admin.miaodong.entity.AppFamily;
import com.vf.admin.miaodong.entity.AppFamilyMember;
import com.vf.admin.miaodong.entity.AppFamilySetLevel;
import com.vf.admin.miaodong.entity.AppUser;
import com.vf.admin.utils.StringEx;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.util.Date;
import java.util.List;

import static com.vf.admin.client.tx.utils.TxIm.portraitTouXiangSet;
import static com.vf.admin.utils.StringEx.isNull;

/**
 * 家族管理
 */
@Controller
@RequestMapping("/after/family")
public class AfterFamilyController extends BaseController {


    @Autowired
    private AppFamilyService appFamilyService;
    @Autowired
    private AppFamilyMemberService appFamilyMemberService;
    @Autowired
    private AppFamilySetLevelService appFamilySetLevelService;
    @Autowired
    private AppUserService appUserService;

    /**
     * 家族列表
     * @param splitPageBean
     * @param appFamily
     * @return
     */
    @RequestMapping("/list")
    public String list(SplitPageBean splitPageBean, AppFamily appFamily)
    {
        List<AppFamily> afterFamilyList = appFamilyService.getAfterFamilyList(splitPageBean, appFamily);
        request.setAttribute("list",afterFamilyList);
        request.setAttribute("splitPage",splitPageBean);
        request.setAttribute("appFamily",appFamily);
        return this.sucess("/web/after/family/list");
    }

    /**
     * 审核家族
     * @param appFamily
     * @return
     */
    @ResponseBody
    @RequestMapping("/verifyFamily")
    public Result verifyFamily(AppFamily appFamily)
    {
        if(StringEx.isNullOrLing(appFamily.getId())){
            return this.errorJSON("id未获取到");
        }
        if(appFamily.getVerifyType()!=1 && appFamily.getVerifyType()!=2){
            return this.errorJSON("审核类型错误");
        }
        AppFamily byIdAll = appFamilyService.findByIdAll(appFamily.getId());
        if(byIdAll==null){
            return this.errorJSON("家族不存在");
        }
        if(byIdAll.getStatus()!=3){
            return this.errorJSON("当前家族不属于待审核状态");
        }
        appFamily.setFamilyid(byIdAll.getFamilyid());
        appFamily.setLeaderid(byIdAll.getLeaderid());
        appFamilyService.verifyFamily(appFamily);
        return this.sucessJSON("操作成功");
    }

    /**
     * 拒绝原因填写页面
     * @param id
     * @return
     */
    @RequestMapping("/refuseFamily")
    public String refuseFamily(Integer id)
    {
        AppFamily appFamily = appFamilyService.findByIdAll(id);
        request.setAttribute("familyInfo",appFamily);
        return this.sucess("/web/after/family/refuseFamily");
    }



    /**
     * 编辑家族数据
     * @param id
     * @return
     */
    @RequestMapping("/editFamily")
    public String editFamily(Integer id)
    {
        AppFamily appFamily = appFamilyService.findByIdAll(id);
        request.setAttribute("familyInfo",appFamily);
        return this.sucess("/web/after/family/editFamily");
    }


    /**
     * 编辑家族操作
     * @param appFamily
     * @return
     */
    @ResponseBody
    @RequestMapping("/editFamilyDo")
    public Result editFamilyDo(AppFamily appFamily)
    {
        if(StringEx.isNullOrLing(appFamily.getId())){
            return this.errorJSON("id未获取到");
        }
        if(StringEx.isNull(appFamily.getFamilyname())){
            return this.errorJSON("请填写家族名称");
        }
        if(StringEx.isNull(appFamily.getFamilynotice())){
//            return this.errorJSON("请填写家族公告");
        }
        if(StringEx.isNull(appFamily.getPic())){
            return this.errorJSON("请上传家族头像");
        }
        AppFamily appFamily1 = appFamilyService.findByIdAll(appFamily.getId());
        if (appFamily1 == null){
            return this.errorJSON("家族不存在");
        }
        appFamily.setUpdatetime(new Date());
        appFamilyService.save(appFamily);
        return this.sucessJSON("编辑成功");
    }



    /**
     * 修改家族状态
     * @param id 家族id
     * @param status 0正常，1禁用
     * @return
     */
    @ResponseBody
    @RequestMapping("/updateFamilyStatus")
    public Result updateFamilyStatus(Integer id,Integer status)
    {
        if(StringEx.isNullOrLing(id)){
            return this.errorJSON("家族id不能为空");
        }
        if(status!=0 && status!=1){
            return this.errorJSON("状态类型错误");
        }
        appFamilyService.saveFamilyStatus(id,status);
        return this.sucessJSON("操作成功");
    }


    /**
     * 家族成员
     * @param splitPageBean
     * @param appFamilyMember
     * @return
     */
    @RequestMapping("/familyMember")
    public String familyMember(SplitPageBean splitPageBean, AppFamilyMember appFamilyMember)
    {
        List<AppFamilyMember> appFamilyMembers = appFamilyMemberService.afterFamilyMemberList(splitPageBean, appFamilyMember);
        request.setAttribute("list",appFamilyMembers);
        request.setAttribute("splitPage",splitPageBean);
        request.setAttribute("appFamilyMember",appFamilyMember);
        return this.sucess("/web/after/family/familyMember");
    }

    /**
     * 家族级别设置
     * @param splitPageBean
     * @return
     */
    @RequestMapping("/familyLevelList")
    public String familyLevelList(SplitPageBean splitPageBean)
    {
        List<AppFamilySetLevel> levelList = appFamilySetLevelService.getLevelList(splitPageBean);
        request.setAttribute("list",levelList);
        request.setAttribute("splitPage",splitPageBean);
        return this.sucess("/web/after/family/familyLevelList");
    }

    /**
     * 添加或修改家族级别页面
     * @return
     */
    @RequestMapping("/saveOrAddLevel")
    public String saveOrAddLevel(AppFamilySetLevel appFamilySetLevel)
    {
        if(StringEx.isNullOrLing(appFamilySetLevel.getId())){
            return this.sucess("/web/after/family/saveOrAddLevel");
        }else{
            AppFamilySetLevel info = appFamilySetLevelService.getInfo(appFamilySetLevel.getId());
            request.setAttribute("info",info);
            return this.sucess("/web/after/family/saveOrAddLevel");
        }
    }



    /**
     * 添加或修改家族级别
     * @param appFamilySetLevel
     * @return
     */
    @ResponseBody
    @RequestMapping("/saveOrAddLevelDo")
    public Result saveOrAddLevelDo(AppFamilySetLevel appFamilySetLevel)
    {
        appFamilySetLevelService.saveOrAddLevel(appFamilySetLevel);
        return this.sucessJSON("操作成功");
    }


    @ResponseBody
    @RequestMapping("/uppic")
    public Result uppic(MultipartFile[] file) {
        try {
            String pic = "";
            //上传：循环获取file数组中得文件
            StringBuffer stringBuffer = new StringBuffer();
            if (file != null && file.length > 0) {
                MultipartFile files = file[0];
                if (!files.isEmpty()) {
                    //判断图片大小
                    if (files.getSize() <= 52428800) {
                        //上传阿里云oss
                        String ossUrl = appUserService.aliyunOssupload(files);
                        if (isNull(ossUrl)) {
                            return errorJSON("图片上传失败");
                        }
                        //拼接
                        stringBuffer.append(ossUrl);
                    } else {
                        return this.errorJSON("图片大小超过限制！");
                    }
                }
            } else {
                return this.errorJSON("图片不能为空");
            }
            pic = stringBuffer.toString();
            return this.sucessJSON(pic, "保存成功");
        } catch (Exception e) {
            return this.errorJSON("系统繁忙");
        }
    }







}
